package com.yanqu.road.server.cmd.strongholdwar;

import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.cross.stronghold.CrossActivityUnionRankModel;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdRankMgr;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.Collection;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_UNION_RANK,desc = "")
public class CrossStrongholdUnionRankCmd extends CrossUnionCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long userId = packet.getPlayerId();
        StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg req = StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        int conditionType = req.getConditionType();
        String unionUid = req.getUnionUid();
        UnionActivityConditionData ucd = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, eGamePlayerEventType.StrongholdWildUnionScoreRank.getValue(), userId);
        if (null != ucd) {
            unionUid = ucd.getUnionUid();
        }
        if (conditionType == CrossStrongholdActivityMgr.CONDITION_TYPE_SILVER_DONAGE && CrossStrongholdActivityMgr.isAfterWildTime(activityId)) {
            sendSilverRank(activityId, unionUid, conditionType, userId);
        }else {
            int unionRank = CrossStrongholdRankMgr.getCrossActivityUnionRank(activityId, conditionType, serverId, unionUid);
            StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder msg = StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.newBuilder();
            msg.setMyRank(unionRank);
            CrossActivityUnionRankModel model = CrossStrongholdRankMgr.getCrossActivityUnionRankListModel(activityId, groupId, conditionType, true);
            BigInteger myUnionValue = model.getMyUnionValue(serverId, unionUid);
            msg.setValue(myUnionValue.toString());
            UnionActivityRankProto.UnionActivityRankListMsg.Builder rankListMsg = model.getRankListMsg();
            msg.setRankList(rankListMsg);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(ClientProtocol.U_STRONGHOLD_UNION_RANK, msg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }
    }

    private void sendSilverRank(int activityId,String unionUid,int conditionType,long userId) {
        UnionActivityRankProto.UnionActivityRankListMsg tempList = CrossStrongholdRankMgr.getCrossActivityUnionRankListModel(activityId, groupId, conditionType, true).getRankListMsg().build();
        StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder msg = StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.newBuilder();
        msg.setMyRank(CrossStrongholdMgr.getSilverRank(activityId, unionUid));
        Collection<String> qualifyUnionList = CrossStrongholdMgr.getQualifyUnionList(activityId, groupId);
        BigInteger myUnionValue = BigInteger.ZERO;
        UnionActivityRankProto.UnionActivityRankListMsg.Builder rankListMsg = UnionActivityRankProto.UnionActivityRankListMsg.newBuilder();
        for (UnionActivityRankProto.UnionActivityRankTempMsg tempMsg : tempList.getRankListList()) {
            if (qualifyUnionList.contains(tempMsg.getUnionUid())) {
                rankListMsg.addRankList(tempMsg);
            }
            if (tempMsg.getUnionUid().equals(unionUid)) {
                myUnionValue = new BigInteger(tempMsg.getValue());
            }
        }
        msg.setValue(myUnionValue.toString());
        msg.setRankList(rankListMsg);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(ClientProtocol.U_STRONGHOLD_UNION_RANK, msg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }



    @Override
    public void sendData() {

    }
}
